texture g_GBufferTexture0;
texture g_GBufferTexture1;
float2 g_ScreenSize_Inv;
float g_AspectRatio;
float g_TanFovY_Half;
float g_Z_Far;

sampler GBufferSampler0 = sampler_state {
	Texture = (g_GBufferTexture0);
	AddressU = CLAMP; AddressV = CLAMP;
	MinFilter = POINT; MagFilter = POINT; MipFilter = NONE;
};

sampler GBufferSampler1 = sampler_state {
	Texture = (g_GBufferTexture1);
	AddressU = CLAMP; AddressV = CLAMP;
	MinFilter = POINT; MagFilter = POINT; MipFilter = NONE;
};

struct GBUFFER_DATA
{
	float3 Pos_View;
	float3 Normal_View;
	float3 Diffuse;
	float SpecularIntensity;
	float SpecularExponent;
};

void SampleGBuffers_Tex(float2 TexCoord, float3 EyeVec, out GBUFFER_DATA Out)
{
	float4 GBufferSample0 = tex2D(GBufferSampler0, TexCoord);
	float4 GBufferSample1 = tex2D(GBufferSampler1, TexCoord);
	
	Out.Pos_View = GBufferSample1.b * EyeVec;
	Out.Normal_View.xy = GBufferSample1.rg * 2 - 1;
	Out.Normal_View.z = -sqrt(1 - Out.Normal_View.x*Out.Normal_View.x - Out.Normal_View.y*Out.Normal_View.y);
	Out.Diffuse = GBufferSample0.rgb;
	Out.SpecularIntensity = GBufferSample0.a;
	Out.SpecularExponent = exp(GBufferSample1.a*10.5);
}

void SampleGBuffers(float2 Pos, float3 EyeVec, out GBUFFER_DATA Out)
{
	// Tu bylo - float2(0.5, 0.5) i okazalo sie zle, wiec zmienilem na +, ale nie wiem czemu tak jest.
	float2 TexCoord = (Pos + float2(0.5, 0.5)) * g_ScreenSize_Inv;
	SampleGBuffers_Tex(TexCoord, EyeVec, Out);
}

void MainVS(
	in float4 In_Pos : POSITION,
	out float4 Out_Pos : POSITION,
	out float3 Out_EyeVec : TEXCOORD0)
{
	Out_Pos = In_Pos;
	Out_EyeVec = float3(
		In_Pos.x * g_TanFovY_Half * g_Z_Far * g_AspectRatio,
		In_Pos.y * g_TanFovY_Half * g_Z_Far,
		g_Z_Far);
}

